package tf2pickup.listeners;

import java.util.ArrayList;
import java.util.HashMap;

import org.pircbotx.hooks.events.MessageEvent;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.PircBotX;

import tf2pickup.Config;
import tf2pickup.GameServer;
import tf2pickup.Match;
import tf2pickup.Players;
import tf2pickup.Tf2Class;

/**
 * This Listener takes care of adding players to the pickup. It will check if 
 * the pickup is full already, the player is added to the pickup already or the 
 * class-limit of the Tf2Class is reached.
 * @author Philipp Preß
 */
public class AddPlayer extends ListenerAdapter<PircBotX> {

    private Players players;
    private HashMap<String, Integer> maps;
    private ArrayList<GameServer> servers;

    /**
     * Creates a new Listener that listens for the well known add-commands.
     * @param players list of players
     * @param maps list of maps and their votes
     * @param servers list of gameserves
     */
    public AddPlayer(Players players, HashMap<String, Integer> maps, ArrayList<GameServer> servers) {
        this.players = players;
        this.maps = maps;
        this.servers = servers;
    }

    /**
     * will attempt to add a player as medic, demoman, soldier, scout when the
     * commands !me, !dem, !so, !sc will be sent to the channel. Adding-process
     * can fail when player is already added, pickup is full or classlimit is 
     * rached.
     * @param event the dispatched event.
     */
    @Override
    public void onMessage(MessageEvent<PircBotX> event) {
        String message = event.getMessage().toLowerCase();
        if (message.startsWith("!me")) {
            this.handle(event, Tf2Class.MEDIC);
        } else if (message.startsWith("!dem")) {
            this.handle(event, Tf2Class.DEMOMAN);
        } else if (message.startsWith("!so")) {
            this.handle(event, Tf2Class.SOLDIER);
        } else if (message.startsWith("!sc")) {
            this.handle(event, Tf2Class.SCOUT);
        }
    }

    private void handle(MessageEvent event, Tf2Class tf2Class) {
        //Check if the player is added already
        if (players.containsPlayer(event.getUser().getNick())) {
            event.respond("Cannot add you to the pickup: You are already added. If "
                    + "you want to change classes, remove yourself first.");
            return;
        }
        //Check if the pickup is full
        if (players.getPlayersCount() >= players.getMaximumSize()) {
            event.respond("Cannot add you to the pickup: Pickup is full.");
            return;
        }
        //Check if the class-limit of the specified Tf2Class is reached
        if (players.getTf2ClassCount(tf2Class) >= (tf2Class.getClassLimit() * 2)) {
            event.respond("Cannot add you to the pickup: All spots for this "
                    + "class are already taken.");
            return;
        }
        players.addPlayer(event.getUser().getNick(), tf2Class);
        event.respond("You've been added to the pickup.");
        //Refresh topic
        event.getBot().getListenerManager().dispatchEvent(
                new MessageEvent<PircBotX>(event.getBot(), event.getChannel(), event.getUser(), "!status"));

        //Creates a new Match when the pickup is full.
        if (players.getMaximumSize() == players.getPlayersCount()) {
            //Start match
            Match match;
            match = new Match(event, players, maps, servers);
            match.start();
        }
    }
}